package com.billsharing.server;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BillsharingRegister
 */
@WebServlet("/BillsharingRegister")
public class BillsharingRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  	@Override
	   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	    {
  			
	        PrintWriter out=response.getWriter();
	        String reg_uname,reg_pwd,reg_fname,reg_lname,reg_email,reg_phone,result;
	        reg_uname=request.getParameter("username");
	        reg_pwd=request.getParameter("password");
	        reg_fname=request.getParameter("firstname");
	        reg_lname=request.getParameter("lastname");
	        reg_email=request.getParameter("emailid");
	        
	        
	    	result=null;
			Connection conn=null;
			
			
	        try
	        {
	            
	            Class.forName ("com.mysql.jdbc.Driver").newInstance();
	            conn = DriverManager.getConnection ("jdbc:mysql://csdb.csc.villanova.edu/auppalap", "auppalap", "fL00355727");
	            System.out.println ("Database connection established");
	            Statement s=conn.createStatement();
	            int i=s.executeUpdate("insert into Users (username, pwd, Firstname, Lastname, emailid) values ('"+reg_uname+"','"+reg_pwd+"','"+reg_fname+"','"+reg_lname+"','"+reg_email+"')");
	     	  if(i>0)
	     	   {
	     		   result="true1";
	     	   }
	     	   else
	     	   {
	     		   result="false1";
	     	   }
	     	   out.print(result);
	     	   
	        }
	        catch (Exception e)
	        {
	            out.print (e.toString());

	        }
	        finally
	        {
	            if (conn != null)
	            {
	                try
	                {
	                	 
	                    conn.close ();
	                    
	                }
	                catch (Exception e) { /* ignore close errors */ }
	            }
	        }
	    }
}